package com.maibow.util.weixin;

import net.sf.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.maibow.entity.weixin.AccessToken;
import com.maibow.entity.weixin.Button;
import com.maibow.entity.weixin.CommonButton;
import com.maibow.entity.weixin.ComplexButton;
import com.maibow.entity.weixin.Menu;
import com.maibow.entity.weixin.ViewButton;
import com.maibow.util.HttpRequestUtils;

/**
 * 菜单管理器类
 * 脉博网络后台管理系统
 * 包名：com.maibow.util.weixin
 * 文件名：WeixinMenuManager.java
 * 作者: 吴比(Wubi)
 * 时间：2016年5月21日-下午3:53:29
 * Copyright@ 2016湖南脉博网络科技有限公司-版权所有
 *
 */
public class WeixinMenuManager {
    public static void main(String[] args) {
        Menu menu = getMenu();
        String requestParameter = JSON.toJSONString(menu);
        System.out.println(requestParameter);

        WeixinUtil weixinUtil = new WeixinUtil();
        AccessToken accessToken;
        try {
            accessToken = weixinUtil.getAccessTokenV3(WeixinConstants.CHUXIAN_PARTY_APPID, WeixinConstants.CHUXIAN_PARTY_SECRET);
            String token = accessToken.getAccessToken();

            String deleteRequestUrl = WeixinConstants.GET_WEIXIN_DELETE_MENU_URL.replace("ACCESS_TOKEN", token);
            JSONObject deleteJson = HttpRequestUtils.httpGet(deleteRequestUrl);
            if (null != deleteJson) {
                try {
                    System.out.println(deleteJson);
                    System.out.println(deleteJson.get("errcode").toString());
                    String errcode = deleteJson.get("errcode").toString();
                    // 如果是正常的就创建菜单
                    if (Integer.valueOf(errcode) == 0) {
                        // 创建菜单
                        String requestUrl = WeixinConstants.GET_WEIXIN_CREATE_MENU_URL.replace("ACCESS_TOKEN", token);
                        JSONObject json = HttpRequestUtils.httpPostV2(requestUrl, requestParameter);
                        if (null != json) {
                            try {
                                System.out.println(json);
                                System.out.println(json.get("errcode").toString());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 组装菜单数据
     * 
     * @return
     */
    private static Menu getMenu() {
        CommonButton btn11 = new CommonButton();
        btn11.setName("现场聊");
        btn11.setType("click");
        btn11.setKey("V1001_TODAY_MUSIC");

        ViewButton btn12 = new ViewButton();
        btn12.setName("初现转盘");
        btn12.setType("view");
        btn12.setUrl("http://w.url.cn/s/AmSVZsc");

        ViewButton btn13 = new ViewButton();
        btn13.setName("最新活动");
        btn13.setType("view");
        btn13.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67982411e015a05f&redirect_uri=http%3A%2F%2Fwww.hnczwl.cn/maibow-global-html5/test/src/index.htm&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect");

        ComplexButton mainBtn3 = new ComplexButton();
        mainBtn3.setName("送大奖");
        mainBtn3.setSub_button(new Button[] { btn12, btn13 });

        /**
         * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
         * 
         * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
         * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，
         * 那么menu应该这样定义：<br>
         * menu.setButton(new Button[] { mainBtn1,
         * mainBtn2, btn33 });
         */
        Menu menu = new Menu();
        menu.setButton(new Button[] { btn11, mainBtn3 });

        return menu;
    }
}